package com.wdc.deliveryrobot.adapter;

import android.content.Context;
import android.media.Image;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.wdc.deliveryrobot.R;
import com.wdc.deliveryrobot.dialog.HintDialog;
import com.wdc.deliveryrobot.model.Line;

import java.util.List;

/**
 * @Author: tianHuiDa
 * @Date: 2024/11/16
 * @Description: 巡航列表适配
 */
public class CruiseSumAdapter extends RecyclerView.Adapter<CruiseSumAdapter.ViewHolder> {

    private List<Line> lineList;

    private Context context;

    private OnItemClickListener listener;

    private ImageView imageViewSelect;

    public CruiseSumAdapter(List<Line> lines,Context context,OnItemClickListener listener){
        this.lineList = lines;
        this.context = context;
        this.listener = listener;
    }

    @NonNull
    @Override
    public CruiseSumAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_cruise_point, parent, false);
        return new CruiseSumAdapter.ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull CruiseSumAdapter.ViewHolder holder, int position) {
        Line line = lineList.get(position);
        holder.textViewName.setText(line.getName());
        holder.textViewType.setText(line.getType());
        holder.textViewSum.setText(line.getSum());
        holder.textViewTime.setText(line.getTime());
        holder.textView.setText(context.getString(R.string.sum_point,String.valueOf(line.getPointList().size())));
        //详情跳转
        holder.linearLayoutMore.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (listener != null){
                    listener.showPoint(line);
                }
            }
        });
        //删除跳转
        holder.linearLayoutDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                HintDialog hintDialog = new HintDialog(context);
                hintDialog.setMessage(context.getString(R.string.delete_line,line.getName()));
                hintDialog.setOkButtonClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (imageViewSelect == holder.imageView){
                            imageViewSelect = null;
                            if (listener != null)
                                listener.selectLine(null);
                        }
                        if (listener != null){
                            listener.deleteData(line);
                        }
                    }
                });
                hintDialog.setCancelButtonClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        hintDialog.dismiss();
                    }
                });
                hintDialog.show();
            }
        });
        //选中
        holder.linearLayoutLine.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (imageViewSelect != null){
                    //之前已经有选中了
                    imageViewSelect.setImageResource(R.drawable.noselectsmall);
                    imageViewSelect = holder.imageView;
                    imageViewSelect.setImageResource(R.drawable.selectsmall);
                }else {
                    imageViewSelect = holder.imageView;
                    imageViewSelect.setImageResource(R.drawable.selectsmall);
                }
                if (listener != null)
                    listener.selectLine(line);
            }
        });
    }

    @Override
    public int getItemCount() {
        return lineList.size();
    }


    public interface OnItemClickListener{
        void showPoint(Line lines);

        void deleteData(Line lines);

        void selectLine(Line lines);

    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        public ImageView imageView;

        public TextView textViewName,textViewType,textViewTime,textViewSum,textView;

        public LinearLayout linearLayoutDelete,linearLayoutMore,linearLayoutLine;


        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.select);
            textViewName = itemView.findViewById(R.id.name_line);
            textViewType = itemView.findViewById(R.id.line_type);
            textViewTime = itemView.findViewById(R.id.stop_time);
            textViewSum = itemView.findViewById(R.id.cruise_sum);
            textView = itemView.findViewById(R.id.sum_point);
            linearLayoutDelete = itemView.findViewById(R.id.delete);
            linearLayoutMore = itemView.findViewById(R.id.more);
            linearLayoutLine = itemView.findViewById(R.id.select_line);
        }
    }

    //更新数据
    public void setLineList(List<Line> lineList){
        this.lineList = lineList;
    }



}
